//将parent环境set到自己的环境中
window.electron = window.top.electron
window.$ = (args) => window.top.$(args, document);
document.notification = window.top.document.notification
document.print = window.top.document.print
document.println = window.top.document.println
document.printclear = window.top.document.printclear
window.GetUUID = window.top.GetUUID
document.Page = window.top.document.Page

const { httpGet, httpPost } = electron.HTTP();
const sql = electron.JDBC().sqlite;
const resultTextarea = document.querySelector("#result");


const setResult = (rs) => {
    resultTextarea.value = rs
}

function jump(url) {
    electron.openBrowerUrl(url)
}

function testGet() {
    let ip = $("#ip").val()
    let port = $("#port").val()
    let uri = $("#uri").val()

    httpGet(ip, port, uri).then(res => {
        if (!res || res == "") {
            document.println("HTTP GET 发生了错误");
            setResult(" ")
        } else {
            document.println("HTTP GET 返回了结果");
            setResult(res);
        }

        document.querySelector("#result").focus();
    }).catch(e => {
        document.println("HTTP GET 发生了错误");
        setResult(e.toString())
        document.querySelector("#result").focus();
    })
}

function testPost() {
    let ip = $("#ip").val()
    let port = $("#port").val()
    let uri = $("#uri").val()
    let params = $("#params").val()
        .replaceAll(" ", "")
        .replaceAll("\r", "")
        .replaceAll("\n", "")
        .replaceAll("\t", "")

    httpPost(ip, port, uri, params).then(res => {
        if (!res || res == "") {
            document.println("HTTP POST 发生了错误");
            setResult(" ")
        } else {
            document.println("HTTP POST 返回了结果");
            setResult(res);
        }

        document.querySelector("#result").focus();
    }).catch(e => {
        document.println("HTTP POST 发生了错误");
        setResult(e.toString())
        document.querySelector("#result").focus();
    })
}

/*
function changeConfigInit() {
    const ip = document.querySelector("#ip")
    const port = document.querySelector("#port")
    const pvftool = document.querySelector("#pvftool");

    let delay = -1; //记录定时器

    const onChange = () => {
        let config = {
            ip: ip.value,
            port: port.value,
            name: pvftool.value,
            version: "",
        }
        let json = JSON.stringify(config);

        //清除计时器，并开始新的计时器
        //如果1000ms内有新的输入则计时器被打断
        //否则运行，执行更新sql
        clearTimeout(delay);
        delay = setTimeout(() => {
            sql.config.replaceInto("pvfTool", json).then(rs => {
                console.log("更新了数据库")
            })

        }, 1000)
    }

    ip.addEventListener("keyup", onChange)
    port.addEventListener("keyup", onChange)
    pvftool.addEventListener("change", onChange)
}
 */

function changeConfigInit() {
    const ip = document.querySelector("#ip")
    const port = document.querySelector("#port")
    const pvftool = document.querySelector("#pvftool");

    pvftool.addEventListener("change", () => {
        if (pvftool.value == "0") {
            ip.value = "localhost"
            port.value = "27000"
            return
        }

        if (pvftool.value == "1") {
            ip.value = "localhost"
            port.value = "2020"
            return
        }
    })
}

function jsonCompr() {
    let params = $("#params")
    let value = params.val()

    value = value
        .replaceAll(" ", "")
        .replaceAll("\r", "")
        .replaceAll("\n", "")
        .replaceAll("\t", "")
    params.val(value)
}

function jsonFt() {
    try {
        let params = $("#params")
        let value = params.val().replaceAll("Null", "\"null\"")
        let json = JSON.parse(value);
        let format = JSON.stringify(json, null, 2).replaceAll("\"null\"", "Null")
        params.val(format)
    } catch (e) {
        console.error(e)
        document.println(e.toString())
    }
}

//@TODO
function rsFt() {
    const result = $("#result")
    try {
        let value = result.val().replaceAll("Null", "\"null\"")
        let json = JSON.parse(value);
        let format = JSON.stringify(json, null, 2).replaceAll("\"null\"", "Null")
        result.val(format)
    } catch (e) {
        console.error(e)
        document.println(e.toString())
    }
}

function rsRep() {
    const result = $("#result")
    try {
        let value = result.val()
            .replace(`#PVF_File`, `\r\n#PVF_File`)
            .replaceAll("\\t", "\t")
            .replaceAll("\\r\\n", "\r\n")
        result.val(value)
    } catch (e) {
        console.error(e)
        document.println(e.toString())
    }
}

//@TODO
function rsUtf8() {
    const result = $("#result")

}

//@TODO
function rsItain1() {
    const result = $("#result")

}
//@TODO
function rsCP949() {
    const result = $("#result")

}

//@TODO
function rsCHT() {
    const result = $("#result")

}

//@TODO
function rsCHS() {
    const result = $("#result")

}

function init() {
    const ip = document.querySelector("#ip")
    const port = document.querySelector("#port")
    const pvftool = document.querySelector("#pvftool");

    //查询链接参数
    sql.config.queryByName("pvfTool").then(rs => {
        if (rs) {
            let config = JSON.parse(rs.value);
            ip.value = config.ip || "localhost";
            port.value = config.port || "27000";
            pvftool.value = config.name || "0"
        } else {
            ip.className = "wrong"
            port.className = "wrong"
            pvftool.className = "wrong"
        }

        changeConfigInit(); //绑定事件，监听配置改变
    })
}

window.onload = function () {
    init();
}